package vip.denglei.company.util.kuidi100;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.kuaidi100.sdk.pojo.HttpResult;
import com.kuaidi100.sdk.response.QueryTrackResp;
import com.kuaidi100.sdk.response.SubscribeResp;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;

import java.util.HashMap;
import java.util.Map;

/**
 * 快递100接口
 *
 * @Author: api.kuaidi100.com
 * @Date: 2020-07-14 16:27
 */
public class KdUtil {
	private static QueryTrackReq getQueryTrackReq(String com, String num, String phone) {
		QueryTrackReq queryTrackReq = new QueryTrackReq();
		QueryTrackParam queryTrackParam = new QueryTrackParam();
		queryTrackParam.setCom(com);
		queryTrackParam.setNum(num);
		queryTrackParam.setPhone(phone);
		String param = new Gson().toJson(queryTrackParam);

		queryTrackReq.setParam(param);
		queryTrackReq.setCustomer(ApiInfoConstant.customer);
		queryTrackReq.setSign(SignUtils.querySign(param, ApiInfoConstant.key, ApiInfoConstant.customer));
		return queryTrackReq;
	}

	/**
	 * 实时查询
	 *
	 * @param subscribeReq
	 * @return
	 */
	public static QueryTrackResp queryTrack(String com, String num, String phone) throws Exception {
		QueryTrackReq queryTrackReq = getQueryTrackReq(com, num, phone);
		HttpResult httpResult = HttpUtils.doPost(ApiInfoConstant.QUERY_URL, queryTrackReq);
		if (httpResult.getStatus() == HttpStatus.SC_OK && StringUtils.isNotBlank(httpResult.getBody())) {
			return new Gson().fromJson(httpResult.getBody(), QueryTrackResp.class);
		}
		return null;
	}

	private static SubscribeReq getSubscribeReq(String com, String num, String phone, String callbackurl) {
		SubscribeParameters subscribeParameters = new SubscribeParameters();
		subscribeParameters.setCallbackurl(callbackurl);
		subscribeParameters.setPhone(phone);
		subscribeParameters.setSalt("1syhl9t");

		SubscribeParam subscribeParam = new SubscribeParam();
		subscribeParam.setParameters(subscribeParameters);
		subscribeParam.setCompany(com);
		subscribeParam.setNumber(num);
		subscribeParam.setKey(ApiInfoConstant.key);

		SubscribeReq subscribeReq = new SubscribeReq();
		subscribeReq.setSchema(ApiInfoConstant.SUBSCRIBE_SCHEMA);
		subscribeReq.setParam(new Gson().toJson(subscribeParam));
		return subscribeReq;
	}

	/**
	 * 订阅接口
	 *
	 * @param callbackurl
	 * @return
	 */
	public static SubscribeResp subscribe(String com, String num, String phone, String callbackurl) throws Exception {
		SubscribeReq subscribeReq = getSubscribeReq(com, num, phone, callbackurl);
		HttpResult httpResult = HttpUtils.doPost(ApiInfoConstant.SUBSCRIBE_URL, subscribeReq);
		if (httpResult.getStatus() == HttpStatus.SC_OK && StringUtils.isNotBlank(httpResult.getBody())) {
			return new Gson().fromJson(httpResult.getBody(), SubscribeResp.class);
		}
		return null;
	}

	/**
	 * 地址解析
	 * @param content
	 * @return
	 * @throws Exception
	 */


	public static Object queryAddressRecognition(String content)  {
		Map<String,Object> requestParams = new HashMap<>();
		requestParams.put("secret_key",ApiInfoConstant.secretKey);
		requestParams.put("secret_code",ApiInfoConstant.SecreCode);
		requestParams.put("secret_sign",ApiInfoConstant.SecretSign);
		requestParams.put("content", content);
		String response=HttpUtil.post(ApiInfoConstant.CLOUD_NORMAL_URL,requestParams);
		JSONObject jsonObject = JSON.parseObject(response);
		if(jsonObject.get("code").equals(HttpStatus.SC_OK)){
			return jsonObject.get("data");
		}
		return jsonObject;
	}

	public static void main(String[] args) {
        try {
			SubscribeResp subscribeReq= subscribe("jd","JD0129530061009","18971614248","https://hbcaiyu.cn/company/appUserOrder/logisticsNotify");
			System.out.println(subscribeReq);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
